﻿// /////////////////////////////////// 
//  
//  
//     (c)Copyright 2001-2010 Xiaojun (David) Chen 
//        Authored and Owned Xiaojun (David) Chen 
//        All Right Reserved
//  
// /////////////////////////////////// 


using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

namespace CCF20Model
{
    
    public class Device
    {

        [ReadOnly(true)]

        [DisplayName("序号")]
        public int DeviceID { get; set; }
        public int? CustomerID { get; set; }
        public int? VehicleID { get; set; }

        [DisplayName("产品类型"), Required]
        public string DeviceType { get; set; }

        [DisplayName("产品序列号"), Required]
        public string DeviceKey { get; set; }

        [DisplayName("IDC号"), Required]
        public string DeviceIDCNo { get; set; }

        [DisplayName("进仓时间")]
        public DateTime? InStockDate { get; set; }

        [DisplayName("售出时间")]
        public DateTime? SaleDate { get; set; }

        [DisplayName("销售员")]
        public string Sales { get; set; }
        
        [DisplayName("客户姓名")]
        public string CustomerName { get; set; }

        [DisplayName("车牌")]
        public string VehicleLicense { get; set; }
        
        [DisplayName("设备状况")]
        public string DeviceStatus  { get; set;}
            
        [DisplayName("销售单号")]
        public string Invoice { get; set; }

        [DisplayName("索赔状况")]
        public string RefundStatus { get; set; }

        [DisplayName("注册状态")]
        public bool isRegistered { get; set; }

        [DisplayName("特殊IDC")]
        public bool isSpecialIDC { get; set; }

        [DisplayName("手动入库")]
        public bool? isManual { get; set; }


    }
    public class VehicleLicenseType
    {
        public int VehicleID { get; set; }
        public string VehicleLicense { get; set; }
    }

    public class DeviceModel
    {
        CCF20Entities ccf20Ent;
        ObservableCollection<Device> deviceObserable;
        public IEnumerable<VehicleLicenseType> PopulateVehicleLicense()
        {
            return from c in ccf20Ent.Vehicles
                   orderby c.VehicleLicense
                   select new VehicleLicenseType
                   {
                       VehicleID = c.VehicleID,
                       VehicleLicense = c.VehicleLicense
                   };

        }
        public bool IsDuplicateDevice(string searchValue,string searchField)
        {
            Device device = null;
            switch (searchField)
            {
                case "DeviceKey":
                    device = ccf20Ent.Devices.Where(d => d.DeviceKey == searchValue).FirstOrDefault();
                    break;
                case "IDCSimNum":
                    device = ccf20Ent.Devices.Where(d => d.IDCSimNumber == searchValue).FirstOrDefault();
                    break;
            }
            
            if (device == null)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        public IQueryable<string> GetVehicleLicense(int IDSID, string text)
        {
            return from v in ccf20Ent.Vehicles
                   join c in ccf20Ent.Customers on v.CustomerID equals c.CustomerID
                   where (c.IDSID == IDSID) && (v.VehicleLicense.StartsWith(text))
                   select v.VehicleLicense;
        }
        public string GetCustomerByVehicleLicense(string license)
        {
            Customer cus = ccf20Ent.Vehicles.Where(v => v.VehicleLicense == license)
                            .Select(v => v.Customer)
                            .FirstOrDefault();
            return String.Format("姓名: {0} | 客户号: {1}| 手机: {2}", cus.Name, cus.CustomerNo, cus.FirstMobile);
        }

        public IQueryable<Device> GetDevice()
        {
            return from vd in ccf20Ent.Devices
                   orderby vd.DeviceID descending
                   select new Device
                   {
                       DeviceID = vd.DeviceID,
                       DeviceIDCNo = vd.IDCSimNumber,
                       CustomerID = vd.Customer.CustomerID,
                       VehicleID = vd.Customer.Vehicles.FirstOrDefault().VehicleID,
                       DeviceStatus = vd.DeviceStatus,
                       DeviceKey = vd.DeviceKey,
                       DeviceType = vd.DeviceType,
                       InStockDate = vd.InStockDate,
                       Invoice = vd.Invoice,
                       Sales = vd.Sales,
                       SaleDate = vd.SaleDate,
                       isRegistered = vd.IsRegistered ?? false,
                       isSpecialIDC = vd.IsSpecialIDC ?? false,
                       CustomerName = vd.Customer.Name ?? string.Empty,
                       VehicleLicense = vd.Customer.Vehicles.FirstOrDefault().VehicleLicense ?? string.Empty,
                       isManual = vd.IsManual
                   };

        }
        public ObservableCollection<Device> GetObservableDevice()
        {
            var query = from vd in ccf20Ent.Devices
                   orderby vd.DeviceID
                   select new Device
                   {
                       DeviceID = vd.DeviceID,
                       DeviceIDCNo = vd.IDCSimNumber,
                       CustomerID = vd.Customer.CustomerID,
                       VehicleID = vd.Vehicle.VehicleID,
                       DeviceStatus = vd.DeviceStatus,
                       DeviceKey = vd.DeviceKey,
                       DeviceType = vd.DeviceType,
                       InStockDate = vd.InStockDate,
                       Invoice = vd.Invoice,
                       Sales = vd.Sales,
                       SaleDate = vd.SaleDate,
                       isRegistered = vd.IsRegistered ?? false,
                       isSpecialIDC = vd.IsSpecialIDC ?? false,
                       CustomerName = vd.Customer.Name ?? string.Empty,
                       VehicleLicense = vd.Vehicle.VehicleLicense ?? string.Empty
                   };
            deviceObserable = new ObservableCollection<Device>(query);
            return deviceObserable;
        }
        public void Delete(int DeviceID)
        {
            Device vdLocal = ccf20Ent.Devices.Where(vd => vd.DeviceID == DeviceID).First();
            ccf20Ent.Devices.DeleteObject(vdLocal);
            ccf20Ent.SaveChanges();
            //deviceObserable.Remove(deviceObserable.Where(d=>d.DeviceID == DeviceID).First());
        }

        public void Update(Device vd)
        {
            Device vdLocal = ccf20Ent.Devices.Where(c => c.DeviceID == vd.DeviceID).FirstOrDefault();
            if (vdLocal != null)
            {
                vdLocal.DeviceStatus = vd.DeviceStatus;
                vdLocal.DeviceKey = vd.DeviceKey;
                vdLocal.DeviceType = vd.DeviceType;
                vdLocal.InStockDate = vd.InStockDate;
                vdLocal.IDCSimNumber = vd.DeviceIDCNo;
                vdLocal.Invoice = vd.Invoice;
                vdLocal.Sales = vd.Sales;
                vdLocal.SaleDate = vd.SaleDate;
                vdLocal.IsRegistered = vd.isRegistered;
                vdLocal.IsSpecialIDC = vd.isSpecialIDC;
                vdLocal.IsManual = vd.isManual;

                if (vd.DeviceStatus == "售出")
                {
                    //for sold, we need to create the device/customer association
                    Customer customer = ccf20Ent.Customers.Where(c => c.CustomerID == vd.CustomerID).FirstOrDefault();
                    if (customer != null)
                    {
                        customer.Devices.Add(vdLocal);
                    }
                    Vehicle vehicle = ccf20Ent.Vehicles.Where(v => v.VehicleID == vd.VehicleID).FirstOrDefault();
                    if (vehicle != null)
                    {
                        vehicle.Devices.Add(vdLocal);
                    }
                    
                }
                if (vd.DeviceStatus == "库存")
                {
                    //for returned device, remove the association.
                    Customer customer = ccf20Ent.Customers.Where(c => c.CustomerID == vd.CustomerID).FirstOrDefault();
                    if (customer != null)
                    {
                        customer.Devices.Remove(vdLocal);
                    }
                    //also remove the vehicle association
                    Vehicle vehicle = ccf20Ent.Vehicles.Where(v => v.VehicleID == vd.VehicleID).FirstOrDefault();
                    if (vehicle != null)
                    {
                        vehicle.Devices.Remove(vdLocal);
                        var vr = from v in ccf20Ent.VehicleMaintenanceRecords
                                where v.VehicleID == vehicle.VehicleID
                                select v;
                        //also remove the vehicle maintenance record, if vehicle is not empty
                        
                        if (vr != null)
                        {
                            foreach (VehicleMaintenanceRecord vmr in vr.ToList())
                            {
                                ccf20Ent.DeleteObject(vmr);
                            }
                        }
                    
                    }
                    
                    
                }

                ccf20Ent.SaveChanges();
            }
        }

        public void Insert(Device vd,bool isManual)
        {

            Device vdLocal = new Device();
            vdLocal.DeviceStatus = vd.DeviceStatus;
            vdLocal.DeviceKey = vd.DeviceKey;
            vdLocal.DeviceType = vd.DeviceType;
            //vdLocal.InStockDate = vd.InStockDate;
            vdLocal.Invoice = vd.Invoice;
            vdLocal.IDCSimNumber = vd.DeviceIDCNo;
            vdLocal.IsManual = isManual;
            //vdLocal.Sales = vd.Sales;
            //vdLocal.SaleDate = vd.SaleDate;

            //vdLocal.IsRegistered = vd.isRegistered;
            //vdLocal.IsSpecialIDC = vd.isSpecialIDC;
            ccf20Ent.Devices.AddObject(vdLocal);
            ccf20Ent.SaveChanges();
        }
        public DeviceModel()
        {
            ccf20Ent = new CCF20Entities();
        }
    }
}
